package com.qzsoft.zhjd.server.modules.asphalt.controller;

import com.alibaba.fastjson.JSONObject;
import com.qzsoft.zhjd.server.common.utils.R;
import com.qzsoft.zhjd.server.modules.asphalt.model.CollectDataModel;
import com.qzsoft.zhjd.server.modules.asphalt.service.CollectAsphaltReceiveService;
import com.qzsoft.zhjd.server.socket.config.TcpReceiveConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author linhuawei
 * @ClassName AsphaltReceiveController
 * @Description 沥青数据接收
 * @since 2022-01-15
 */
@Slf4j
@RestController
@RequestMapping("/datacollect/asphalt")
public class CollectAsphaltReceiveController {

    @Autowired
    private CollectAsphaltReceiveService collectAsphaltReceiveService;

    @PostMapping("/receive")
    public R receive(@RequestBody String msg) {
        try {
            CollectDataModel model = JSONObject.parseObject(msg, CollectDataModel.class);
            if (TcpReceiveConstants.TYPE_ASPHALT_DATA.equals(model.getType())) {
                log.info("沥青拌合站数据：{}", msg);
                collectAsphaltReceiveService.receive(model);
                return R.ok(model.getId());
            }
            log.error("沥青拌合站数据类型错误：{}", msg);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("沥青拌合站数据处理失败：{}", msg);
        }
        return R.error();
    }

}
